package com.zyl.points;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.zyl.points.bean.Task;
import com.zyl.points.service.TaskService;
import com.zyl.points.util.DBHelper;


public class Plan extends Activity {
	
	private DBHelper db;
	private Cursor myCursor;
	private ListView myListView;
	private EditText myEditText;
	private int _id;
	protected final static int MENU_ADD=Menu.FIRST;
	protected final static int MENU_EDIT=Menu.FIRST+1;
	protected final static int MENU_DELETE=Menu.FIRST+2;
	
	   @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		   super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_ADD, 0,"添加");
		menu.add(Menu.NONE, MENU_EDIT, 0,"编辑");
		menu.add(Menu.NONE, MENU_DELETE, 0,"删除");
		 return true;
	}
	
	   @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		super.onOptionsItemSelected(item); 
		switch (item.getItemId()) {
		case MENU_ADD:
			operation("add");
			break;
		case MENU_EDIT:
			operation("edit");
			break;
		case MENU_DELETE:
			operation("delete");
			break;
		default:
			break;
		}
		return true;
	}
	   
	  private TaskService taskService; 
	  private Task  task; 
	   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task);
        myEditText=(EditText)findViewById(R.id.EditText1);
        myListView=(ListView)findViewById(R.id.ListView1);
        taskService=new TaskService(Plan.this);
        task=taskService.get();
        myListView.setAdapter(new ArrayAdapter<String>(this,R.layout.tasklist,new String[]{"开始问题:"+task.getPage(),"每组数量:"+task.getSize(),"出现次数:"+task.getAppearTimes()}));
        myListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				 
				myCursor.moveToPosition(arg2);
				_id=myCursor.getInt(0);
				myEditText.setText(myCursor.getString(1));
			}
		});
        
        
        myListView.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				 
				SQLiteCursor sc=(SQLiteCursor)arg0.getSelectedItem();
				_id=sc.getInt(0);
				myEditText.setText(sc.getString(1));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				 
				
			}
		});
    }
    private void operation(String cmd)
    {
    	if(myEditText.getText().toString().equals(""))
    	return;
    	if(cmd=="add")
    	//	db.insert( myEditText.getText().toString());
    	if(cmd=="edit")
    	//	db.update(_id,  myEditText.getText().toString());
    	if(cmd=="delete")
    	//	db.delete(_id);
    	myCursor.requery();
    	myListView.invalidateViews();
    	myEditText.setText("");
    	_id=0;
    	
    }
}